const Service = require('egg').Service;

class userDBService extends Service {
    async listAll() {
        //查询用户所有信息
        //let result = await this.app.mysql.select('t_user');
        let queryString = 'select * from t_user u inner join t_department d on u.department_id = d.id '
        let result = await this.app.mysql.query(queryString)
        return result;
    }

    async listAlluser() {
        //查询要导出用户所有信息
        let result = await this.app.mysql.select('t_user');
        return result;
    }

    //按条件查询用户
    async listuser(user) {
        let aa = Object.keys(user)[0]
        let bb = Object.keys(user)[1]
        let cc = Object.keys(user)[2]
        if (Object.keys(user).length === 1) {
            let queryString = 'select * from t_user where ' + aa + ' like ?'
            let params = [user[aa] + '%'];
            console.log(params)
            let result = await this.app.mysql.query(queryString, params);
            return result;
        } else if (Object.keys(user).length === 2) {
            let queryString = 'select * from t_user where ' + aa + ' like ? and ' + bb + ' like ?'
            let params = [user[aa] + '%', user[bb] + '%'];
            console.log(params)
            let result = await this.app.mysql.query(queryString, params);
            return result;
        } else if (Object.keys(user).length === 3) {
            let queryString = 'select * from t_user where ' + aa + ' like ? and ' + bb + ' like ? and ' + cc + ' like ?'
            let params = [user[aa] + '%', user[bb] + '%', , user[cc] + '%'];
            let result = await this.app.mysql.query(queryString, params);
            return result;
        }
    }

    //添加用户
    async addNewuser(vuser) {
        let user = await this.listAll();
        let arr = [];
        for (let i = 0; i < user.length; i++) {
            let falg = (user[i].username === vuser.username)
            arr.push(falg);
        }
        console.log(arr)
        if (arr.indexOf(true) == -1) {
            vuser.status = 0;
            vuser.create_time = this.app.mysql.literals.now;
            vuser.type = 1;
            let result = await this.app.mysql.insert('t_user', { ...vuser });
            return result;
        } else {
            let result = 0
            return result;
        }
    }

    //编辑数据
    async edituser(user) {
        let queryString = 'select * from t_user where id = ?'
        let params = [user.id]
        let result = await this.app.mysql.query(queryString, params);
        return result;
    }

    //更新数据
    async updateTuser(user) {
        user.modified_time = this.app.mysql.literals.now;
        let result = await this.app.mysql.update('t_user', { ...user }, {
            where: { id: user.id }
        });
        return result;
    }

    //删除数据
    async deleteTuser(user) {
        let result = await this.app.mysql.delete('t_user', { id: user.id });
        return result;
    }

    //修改用户状态
    async updatestates(user) {
        user.modified_time = this.app.mysql.literals.now;
        let queryString = 'update t_user set status = ? where id = ?'
        let params = [user.status, user.id]
        let result = await this.app.mysql.query(queryString, params)
        return result
    }

    //查询当前用户所拥有的角色
    async userrole(user) {
        let queryString = 'select r.id,r.role_name from t_role r left join t_user_role ur on ur.role_id =r.id right join t_user t on t.id = ur.user_id where t.id = ?'
        let params = [user.id]
        let result = await this.app.mysql.query(queryString, params)
        return result;
    }

    //查询当前用户未拥有的角色 
    async notuserrole(user) {
        let queryString = 'select id,role_name from t_role where role_name not in (select r.role_name from t_role r left join t_user_role ur on ur.role_id =r.id right join t_user t on t.id = ur.user_id where t.id = ?)'
        let params = [user.id]
        let result = await this.app.mysql.query(queryString, params)
        return result;
    }

    async adduserrole(user) {
       
       
        let queryString = 'insert into t_user_role values (?,?)'
        let params = [user[Object.keys(user)[0]],...user[Object.keys(user)[1]]]
        let result = await this.app.mysql.query(queryString,params)
        return result;
      //let result = await this.app.mysql.insert('t_user_role', { ...user });
    
    }
}

module.exports = userDBService;